package com.sky.controller.test1;

public class Test04 {
    public static void main(String[] args) {
        //单例
        Single instance = Single.getInstance();
        Single instance1 = Single.getInstance();
        System.out.println(instance==instance1);
        B1 lazy = B1.getInstance1();
        B1 lazy1 = B1.getInstance1();
        System.out.println(lazy1==lazy);

    }
}
class  Single{
    //饿汉式单例:拿对象的时候,对象已经有了
    //1.私有化构造器
    private Single(){
        System.out.println(11111);
    }
    //2.定义一个静态变量用于记录本类的唯一对象
//    public static final Single a = new Single();
    private static  Single a = new Single();
    //3.定义一个静态方法，返回唯一对象
    public static Single getInstance(){
        return a;
    }
}
class B1 {
//   懒汉模式
   
    
    private B1(){
        System.out.println(2222);
    }
    private static B1 b;
    public static B1 getInstance1(){
        if(b==null){
            b = new B1();
        }
            return b;
        
    }
}
